﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lib
{
    public class ComBoBoxUtil
    {
        public static bool isSelect(ComboBox comboBox,String title)
        {
            if (comboBox.SelectedIndex <= 0)
            {
                comboBox.Focus();
                CommonUtil.showMessageBox("请选择" + title);
                return false;
            }
            return true;
        }

        public static bool isSelect(ComboBox comboBox)
        {
            if (comboBox.SelectedIndex <= 0)
            {
                comboBox.Focus();
                return false;
            }
            return true;
        }

        public static void bindComboBox(ComboBox comboBox,DataSet ds,String keyField,String valueField)
        {
            SandData sandData = new SandData();
            sandData.Key = "-1";
            sandData.Val = "请选择";
            ArrayList dataList = new ArrayList();
            dataList.Add(sandData);
            
            if (CommonUtil.checkDataSet(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sandData = new SandData();
                    sandData.Key = dr[keyField].ToString();
                    sandData.Val = dr[valueField];
                    dataList.Add(sandData);
                }
            }
            comboBox.DataSource = dataList;
            comboBox.DisplayMember = "Val";
            comboBox.ValueMember = "Key";
        }

        public static void bindComboBox(DataGridViewComboBoxColumn comboBoxColumn, DataSet ds, String keyField, String valueField)
        {
            SandData sandData = new SandData();
            sandData.Key = "-1";
            sandData.Val = "";
            ArrayList dataList = new ArrayList();
            dataList.Add(sandData);

            if (CommonUtil.checkDataSet(ds))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sandData = new SandData();
                    sandData.Key = dr[keyField].ToString();
                    sandData.Val = dr[valueField];
                    dataList.Add(sandData);
                }
            }
            comboBoxColumn.DataSource = dataList;
            comboBoxColumn.DisplayMember = "Val";
            comboBoxColumn.ValueMember = "Key";
        }

        public static void bindComboBox(ComboBox comboBox, Hashtable hashTable)
        {
            ArrayList dataList = new ArrayList();
            SandData sandData = new SandData();
            sandData.Key = "-1";
            sandData.Val = "请选择";
            dataList.Add(sandData);

            foreach (DictionaryEntry dic in hashTable)
            {
                sandData = new SandData();
                sandData.Key = dic.Key.ToString();
                sandData.Val = dic.Value;
                dataList.Add(sandData);
            }
            comboBox.DataSource = dataList;
            comboBox.DisplayMember = "Val";
            comboBox.ValueMember = "Key";
        }
    }
}
